import type { User, Role, Permission } from "./types"

// Mock permissions data
export const mockPermissions: Permission[] = [
  // User management permissions
  {
    id: "perm-1",
    name: "查看用户",
    description: "查看用户列表和详情",
    resource: "users",
    action: "read",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
  {
    id: "perm-2",
    name: "创建用户",
    description: "创建新用户账户",
    resource: "users",
    action: "create",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
  {
    id: "perm-3",
    name: "编辑用户",
    description: "编辑用户信息",
    resource: "users",
    action: "update",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
  {
    id: "perm-4",
    name: "删除用户",
    description: "删除用户账户",
    resource: "users",
    action: "delete",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
  // Role management permissions
  {
    id: "perm-5",
    name: "查看角色",
    description: "查看角色列表和详情",
    resource: "roles",
    action: "read",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
  {
    id: "perm-6",
    name: "创建角色",
    description: "创建新角色",
    resource: "roles",
    action: "create",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
  {
    id: "perm-7",
    name: "编辑角色",
    description: "编辑角色信息",
    resource: "roles",
    action: "update",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
  {
    id: "perm-8",
    name: "删除角色",
    description: "删除角色",
    resource: "roles",
    action: "delete",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
  // Permission management permissions
  {
    id: "perm-9",
    name: "查看权限",
    description: "查看权限列表和详情",
    resource: "permissions",
    action: "read",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
  {
    id: "perm-10",
    name: "管理权限",
    description: "分配和撤销权限",
    resource: "permissions",
    action: "manage",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
  // Dashboard permissions
  {
    id: "perm-11",
    name: "查看仪表板",
    description: "访问管理仪表板",
    resource: "dashboard",
    action: "read",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
  {
    id: "perm-12",
    name: "查看统计",
    description: "查看系统统计数据",
    resource: "analytics",
    action: "read",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
  },
]

// Mock roles data
export const mockRoles: Role[] = [
  {
    id: "role-1",
    name: "超级管理员",
    description: "拥有系统所有权限的最高管理员",
    color: "#dc2626",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
    permissionIds: mockPermissions.map((p) => p.id), // All permissions
  },
  {
    id: "role-2",
    name: "管理员",
    description: "拥有用户和角色管理权限的管理员",
    color: "#2563eb",
    isSystem: true,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
    permissionIds: ["perm-1", "perm-2", "perm-3", "perm-5", "perm-6", "perm-7", "perm-11", "perm-12"],
  },
  {
    id: "role-3",
    name: "编辑者",
    description: "可以查看和编辑用户信息",
    color: "#059669",
    isSystem: false,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
    permissionIds: ["perm-1", "perm-3", "perm-5", "perm-11"],
  },
  {
    id: "role-4",
    name: "查看者",
    description: "只能查看系统信息，无编辑权限",
    color: "#7c3aed",
    isSystem: false,
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
    permissionIds: ["perm-1", "perm-5", "perm-9", "perm-11"],
  },
]

// Mock users data
export const mockUsers: User[] = [
  {
    id: "user-1",
    email: "admin@example.com",
    name: "系统管理员",
    avatar: "/admin-avatar.png",
    status: "active",
    createdAt: new Date("2024-01-01"),
    updatedAt: new Date("2024-01-01"),
    lastLoginAt: new Date(),
    roleIds: ["role-1"],
  },
  {
    id: "user-2",
    email: "manager@example.com",
    name: "部门经理",
    avatar: "/manager-avatar.png",
    status: "active",
    createdAt: new Date("2024-01-15"),
    updatedAt: new Date("2024-01-15"),
    lastLoginAt: new Date(Date.now() - 2 * 60 * 60 * 1000), // 2 hours ago
    roleIds: ["role-2"],
  },
  {
    id: "user-3",
    email: "editor@example.com",
    name: "内容编辑",
    avatar: "/editor-avatar.png",
    status: "active",
    createdAt: new Date("2024-02-01"),
    updatedAt: new Date("2024-02-01"),
    lastLoginAt: new Date(Date.now() - 24 * 60 * 60 * 1000), // 1 day ago
    roleIds: ["role-3"],
  },
  {
    id: "user-4",
    email: "viewer@example.com",
    name: "访客用户",
    avatar: "/viewer-avatar.jpg",
    status: "active",
    createdAt: new Date("2024-02-15"),
    updatedAt: new Date("2024-02-15"),
    lastLoginAt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), // 1 week ago
    roleIds: ["role-4"],
  },
  {
    id: "user-5",
    email: "inactive@example.com",
    name: "停用用户",
    status: "inactive",
    createdAt: new Date("2024-03-01"),
    updatedAt: new Date("2024-03-01"),
    roleIds: ["role-4"],
  },
]

// Helper functions to work with mock data
export const getUserById = (id: string): User | undefined => {
  return mockUsers.find((user) => user.id === id)
}

export const getRoleById = (id: string): Role | undefined => {
  return mockRoles.find((role) => role.id === id)
}

export const getPermissionById = (id: string): Permission | undefined => {
  return mockPermissions.find((permission) => permission.id === id)
}

export const getUserRoles = (user: User): Role[] => {
  return user.roleIds.map((roleId) => getRoleById(roleId)).filter(Boolean) as Role[]
}

export const getUserPermissions = (user: User): Permission[] => {
  const roles = getUserRoles(user)
  const permissionIds = new Set<string>()

  roles.forEach((role) => {
    role.permissionIds.forEach((permId) => permissionIds.add(permId))
  })

  return Array.from(permissionIds)
    .map((permId) => getPermissionById(permId))
    .filter(Boolean) as Permission[]
}

export const hasPermission = (user: User, resource: string, action: string): boolean => {
  const permissions = getUserPermissions(user)
  return permissions.some((perm) => perm.resource === resource && perm.action === action)
}

export const hasRole = (user: User, roleName: string): boolean => {
  const roles = getUserRoles(user)
  return roles.some((role) => role.name === roleName)
}
